<?php
namespace app\index\controller;
use think\Controller;
use think\View;
use app\index\model\User as Usermodel;

class Register extends Controller
{
    public function index()
    {
        $view=new View();
        return $view->fetch('index');
    }

    public function register()
    {
        $user = new Usermodel();
        $user->username = input('username');
        $user->password = input('password');
        $user->status   = input('status');
        $user->email    = input('email');
        $result = $this->validate(
            [
                'name'  => $user->username,
                'email' => $user->email,
                'password' => $user->password,
                'status' => $user->status,
            ],
            [
                'name'  => 'require|max:25',
                'email'   => 'email',
                'password' =>'require|max:16',
                'status' => 'require|max:1',
            ]);
        if(true !== $result){
            // 验证失败 输出错误信息
            return $this->error($result);
        }
        if($user->save()){
            return $this->success('注册成功','login/index');
        }else{
            return $this->error('注册失败');
        }
    }
}